<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/6/7
 * Time: 10:28
 */

namespace Biz\model\smallenergy;


class SmallEnergyMessage extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_im';

    const _CONFIG_SELECT_ = 'db_im';

    const _DATABASE_ = 'hhz_im';

    const _TABLE_NAME = 'hhz_smallenergy_message';

    const MESSAGE_TYPE_TEXT  = 1;
    const MESSAGE_TYPE_IMAGE = 2;
    const MESSAGE_TYPE_FILE  = 3;

    public static $messageType = [
        self::MESSAGE_TYPE_TEXT => '字符',
        self::MESSAGE_TYPE_IMAGE => '图片',
        self::MESSAGE_TYPE_FILE => '文件',
    ];

    const STATUS_UNREAD = 0;
    const STATUS_READ   = 1;

    public $read = [
        self::STATUS_UNREAD => '未读消息',
        self::STATUS_READ => '已读消息'
    ];

    public static function addMessage($data)
    {
        return self::insert($data)->rowcount();
    }

    public static function updateIsreadByUid($uid, $shopId)
    {
        return self::update(['isread' => self::STATUS_READ], [
            'to_uid' => $uid,
            'from_uid' => $shopId,
            'isread' => self::STATUS_UNREAD,
            ]
        )->rowcount();
    }

    public static function getUnreadMessageByUid($uid)
    {
        return self::select('*',[
            'AND' => [
                'to_uid' => $uid,
                'isread' => self::STATUS_UNREAD,
            ],
            'ORDER' => ['sendtime' => 'ASC']
        ]);
    }

    public static function getUnreadCountByUid($uid, $groupId)
    {
        return self::count('*',[
            'to_uid' => $uid,
            'group_id' => $groupId,
            'isread' => self::STATUS_UNREAD,
        ]);
    }

    public static function FixMessage()
    {
        return self::select('*',[
            'group_id' => ''
        ]);
    }

    public static function FixUpdate($where, $settingId, $shopId)
    {
        return self::update(['group_id' => $settingId,'from_uid'=>$shopId], [
                'small_from_to_uid' =>$where
            ]
        )->rowcount();
    }

    public static function getUnreadCount()
    {
        return self::select('*', [
            'isread' => self::STATUS_UNREAD,
        ]);
    }
}